//
//  DrawerViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/7/14.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//
#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height

#import "DrawerViewController.h"
#import "DrawTableViewCell.h"
#import "InformationViewController.h"
#import "LoginRegisterViewController.h"
#import "FeedBackViewController.h"



@interface DrawerViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *tableView;
    NSDictionary *tableDic;
    NSArray *tableDataSource;
    NSDictionary *UnreadNum;
}

@end

@implementation DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}
- (void)setUI{
    CGSize imageSize ;
    if (IS_IPHONE_4) {
        imageSize = CGSizeMake(60, 60);
    }else{
        imageSize = LRLCGSizeMake(60,60);
    }
    
    UIButton *headImage = [UIButton buttonWithType:UIButtonTypeCustom];
    [headImage setImage:[UIImage imageNamed:@"head"] forState:UIControlStateNormal];
    [headImage addTarget:self action:@selector(mySelfInfor) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:headImage];
    [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(45*FitSize);
        make.left.mas_equalTo(20*FitSize);
        make.size.mas_equalTo(imageSize);
    }];
    headImage.layer.cornerRadius = 30*FitSize;
    headImage.layer.masksToBounds = YES;
    
    UILabel *userName = [[UILabel alloc]init];
    userName.text = [UserInfo getUserName];
    userName.font = FONT(15);
    userName.textColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    [self.view addSubview:userName];
    [userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(55*FitSize);
        make.left.equalTo(headImage.mas_right).offset(10);
    }];
    
    UILabel *companyName = [[UILabel alloc]init];
    companyName.text = [UserInfo getDepName];
    companyName.font = FONT(15);
    companyName.textColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    [self.view addSubview:companyName];
    [companyName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userName.mas_bottom).offset(5);
        make.left.equalTo(userName);
    }];
    
    UIView *greenView = [[UIView alloc]init];
    [headImage addSubview:greenView];
    [greenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(headImage);
        make.size.mas_equalTo(CGSizeMake(8, 8));
        make.bottom.mas_equalTo(0);
    }];
    greenView.layer.cornerRadius = 4;
    greenView.layer.masksToBounds = YES;
    greenView.backgroundColor = [UIColor greenColor];
    
    tableView = [[UITableView alloc]initWithFrame:LRLCGRectMake(0,140, WIDTH,HEIGHT-140)];
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor clearColor];
    tableDataSource = @[@"个人资料",@"修改密码",@"意见反馈"];
    NSArray *imageDataSource = @[@"iconfont-information",@"receiving",@"work"];
    tableDic = @{@"arrayDataSource":tableDataSource,@"imageDataSource":imageDataSource};
    
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.backgroundColor = [UIColor colorWithWhite:0.953 alpha:0.5];
    [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(mySelfInfor) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(40*FitSize);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(self.view.bounds.size.width/5*4);
    }];
    
}

- (void)getUnreadNum{
    NSString *ashx = @"/Server/TotalInfoHandler.ashx";
    NSDictionary *dic = @{@""@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"获取主界面未读留言数:%@",model);
                UnreadNum = model[@"data"][0];
                [self setUI];
                
                
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
                
            }
        }else{
            NSLog(@"错误%@",error);
            [XHToast showBottomWithText:@"请求失败"];
        }
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
    NSString *viewTag = [NSString stringWithFormat:@"%ld",indexPath.row];
    NSNotification *notice = [NSNotification notificationWithName:@"pushViewController" object:@{@"PushTo":viewTag} userInfo:nil];
    [[NSNotificationCenter defaultCenter] postNotification:notice];
    
    
    
}
- (void)backApp{
    
    
    exit(0);
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return tableDataSource.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 45*FitSize;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellId = @"drawTabelCell";
    DrawTableViewCell *drawTableCell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!drawTableCell) {
        drawTableCell = [[DrawTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    drawTableCell.backgroundColor = [UIColor clearColor];
    drawTableCell.selectionStyle = UITableViewCellSelectionStyleNone;
    [drawTableCell configWithDic:tableDic indexPath:indexPath.row];
    return drawTableCell;
}

- (void)mySelfInfor{
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"getUserInfo"];
    //    [self dismissViewControllerAnimated:YES completion:nil];
    
    LoginRegisterViewController *login = [[LoginRegisterViewController alloc]init];
    [self presentViewController:login animated:YES completion:nil];
    
}



@end
